package com.viu.technology.service.order;

import com.viu.technology.dao.OrderDao;
import com.viu.technology.dao.product.ProductDaoImpl;
import com.viu.technology.po.Order;
import com.viu.technology.po.Product;
import com.viu.technology.util.GenerateUtil;
import com.viu.technology.util.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductDaoImpl productDao;

    @Autowired
    private OrderDao orderDao;


    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);


    /**
     * 先扣减库存，扣减库存代表下单成功
     * 由于MySQL的update语句会自动锁行，所以只需要在SQL中控制库存数量不少于0即能防止超卖问题
     * @param order
     * @return
     */
    @Transactional
    @Override
    public Order pessimismGenerateOrder(Order order) {


        long startTime = System.currentTimeMillis();
        int isSuc = productDao.updateProductStockPessimism(order);

        if (isSuc <1) {
            return null;
        }

        long firstTime = System.currentTimeMillis();
        Product product = productDao.selectProductById(order.getProductId());
        log.info("获取商品用时："+(System.currentTimeMillis()-firstTime));
        order.setOrderNo(GenerateUtil.generateUUID());
        //待付款
        order.setOrderStatus(1);
        //由于没有地址表，随机
        //order.setAddressId(156);
        order.setOrderAmountTotal(product.getProductPrice()*order.getProductCount());
        order.setProductAmountTotal(product.getProductPrice()*order.getProductCount());
        order.setUserId(SecurityUtil.getUserId());

        long secTime = System.currentTimeMillis();
        int i = orderDao.insertOrder(order);


        log.info("插入商品用时："+(System.currentTimeMillis()-secTime));
        if (i > 0) {
            return order;
        }

        return null;
    }

    /**
     * 使用Redis方式不会更新商品库存，商品库存需要预扣放到Redis中
     @param order
     * @return
     */
    @Transactional
    @Override
    public Order redisGenerateOrder(Order order) {

        /*int isSuc = productDao.updateProductStockPessimismNotImpose(order);

        if (isSuc <1) {
            return null;
        }*/
        long firstTime = System.currentTimeMillis();
        Product product = productDao.selectProductById(order.getProductId());
        log.info("获取商品用时："+(System.currentTimeMillis()-firstTime));
        order.setOrderNo(GenerateUtil.generateUUID());
        //待付款
        order.setOrderStatus(1);
        //由于没有地址表，随机
        //order.setAddressId(156);
        order.setOrderAmountTotal(product.getProductPrice()*order.getProductCount());
        order.setProductAmountTotal(product.getProductPrice()*order.getProductCount());
        order.setUserId(SecurityUtil.getUserId());

        long secTime = System.currentTimeMillis();
        int i = orderDao.insertOrder(order);

        if ((order.getAddressId() > 120 && order.getAddressId() < 130) || (order.getAddressId() > 1012 && order.getAddressId() < 1021)) {
            throw new RuntimeException("异常");
        }

        log.info("插入商品用时："+(System.currentTimeMillis()-secTime));
        if (i > 0) {
            return order;
        }

        return null;
    }
}
